package com.sanjin.blockchain.common.encryptdecrypt;

import com.sanjin.blockchain.common.exceptions.UnsupportedDecryptException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Sha256 implements EncryptDecrypt{

    private static MessageDigest messageDigest;
    private static final String algorithmName = "SHA-256";

    static {
        try {
            messageDigest = MessageDigest.getInstance(algorithmName);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    @Override
    public byte[] encrypt(byte[] bytes) {
        messageDigest.update(bytes);
        return messageDigest.digest();
    }

    @Override
    public byte[] decrypt(byte[] bytes) {
        throw new UnsupportedDecryptException("this algorithm unsupported decrypt!");
    }

    public static void main(String[] args) {

    }
}
